package com.leyou.item.web;

import com.leyou.common.vo.PageResult;
import com.leyou.item.dto.SkuDTO;
import com.leyou.item.dto.SpuDTO;
import com.leyou.item.dto.SpuDetailDTO;
import com.leyou.item.service.GoodsService;
import org.assertj.core.api.IntArrayAssert;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.HttpStatus;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.*;

import java.security.PublicKey;
import java.util.List;


/**
 * @author fenglin
 * @date 2019/12/11 21:29
 */
@RestController

public class GoodsController {
    @Autowired
    private GoodsService goodsService;

    /**
     * 分页查询商品
     * @return
     */
    @GetMapping("/spu/page")
    public ResponseEntity<PageResult<SpuDTO>> querySpuByPage(
            @RequestParam(value = "key",required = false) String key,
            @RequestParam(value = "rows",defaultValue ="5")Integer rows,
            @RequestParam(value = "page", defaultValue = "1")Integer page,
            @RequestParam(value = "saleable",required = false)Boolean saleable
    ){
       PageResult<SpuDTO> result= goodsService.querySpuByPage(key,page,rows,saleable);
       return ResponseEntity.ok(result);
    }
    /**
     * 新增商品
     *
     * @param spuDTO
     * @return
     */
    @PostMapping("goods")
    public ResponseEntity<Void> saveGoods(@RequestBody SpuDTO spuDTO){
        goodsService.saveGoods(spuDTO);
        return ResponseEntity.status(HttpStatus.CREATED).build();
    }

    /**
     * 修改
     *
     * @param spuDTO
     * @return
     */
    @PutMapping("goods")
    public ResponseEntity<Void> updateGoods(@RequestBody SpuDTO spuDTO){
        goodsService.updateGoods(spuDTO);
        return ResponseEntity.status(HttpStatus.CREATED).build();
    }
    /**
     * 上下架
     * @param id
     * @param saleable
     * @return
     */
    @PutMapping("/spu/saleable")
    public ResponseEntity<Void> updateSpuSaleable(@RequestParam("id")Long id,@RequestParam("saleable")Boolean saleable){
        goodsService.updateSpuSaleable(id,saleable);
        // 更新成功，返回204
        return ResponseEntity.status(HttpStatus.NO_CONTENT).build();
    }

    /**
     * 根据id查询spu
     * @param id
     * @return
     */
    @GetMapping("/spu/{id}")
    public  ResponseEntity<SpuDTO> querySpuById(@PathVariable("id")Long id){
        return ResponseEntity.ok(goodsService.querySpuById(id));
    }

    /**
     * 根据spu的id查询spuDetail
     * @param id  spu 的id
     * @return
     */
    @GetMapping("/spu/detail")
    public ResponseEntity<SpuDetailDTO> queryDetailById(@RequestParam("id") Long id){
        return ResponseEntity.ok(goodsService.queryDetailBySpuId(id));
    }

    /**
     * 根据spu的id查询sku的集合
     * @param id spu的id
     * @return sku的集合
     */
    @GetMapping("/sku/of/spu")
    public ResponseEntity<List<SkuDTO>> querySkuBySpuId(@RequestParam("id")Long id){
        return ResponseEntity.ok(  goodsService.querySkuBySpuId(id));
    }
}
